﻿#region Version Info
/* ========================================================================
* 【功能概述】
* 
* 作者：Zhai Xianwen       时间：2012-3-22 13:15:24
* 邮箱 : zhaisky@163.com
* 
* 文件名：Equipment
* 版本：V1.0.1
*
* 修改者：           时间：              
* 修改说明：
* ========================================================================
*/
#endregion

using System;
using System.Collections.Generic;
using System.Net;
using ZXW.DotNet.Common.Basic.Threading.Engines.WorkerEngine;
using ZXW.DotNet.Common.NetSnmp;

namespace WorkEngineTest
{
    public abstract class Agent
    {
        protected static Dictionary<Oid, Parameter> PublicParameters;

        protected Dictionary<Oid, Parameter> PrivateParameters;

        protected static readonly WorkerEngine<Agent> WorkerEngine ;

        public event EventHandler<AgentOnlineChangedEventArgs> OnlineChanged;

        private void OnOnlineChanged(AgentOnlineChangedEventArgs e)
        {
            if (OnlineChanged != null)
                OnlineChanged(this, e);
        }

        public IPAddress IpAddress { get; set; }
        public int Port { get; set; }
        public string ReadCommunity { get; set; }
        public string WriteCommunity { get; set; }
        public string SysDescr { get; private set; }
        public string SysObjectId { get; set; }
        public string SysContect { get; set; }
        public string SysName { get; set; }
        public string SysLocation { get; set; }
        public string SysUpTime { get; private set; }

        protected bool IsOnline { get; private set; }


        #region 构造函数

        static Agent()
        {
            PublicParameters = new Dictionary<Oid, Parameter> {{new Oid(".1.3.6.1.2.1.1.1.0"), new Parameter()}};
            WorkerEngine = new WorkerEngine<Agent> {WorkerThreadCount = 5, WorkProcesser = new WorkProcesser()};
            WorkerEngine.Initialize();
            WorkerEngine.Start();
        }

        /// <summary>
        ///     构造函数
        /// </summary>
        protected Agent()
            : this(IPAddress.Parse("127.0.0.1"), 161, "public", "private")
        {

        }

        /// <summary>
        ///     构造函数
        /// </summary>
        /// <param name="ipAddress" type="System.Net.IPAddress">
        ///     <para>
        ///         设备IP
        ///     </para>
        /// </param>
        protected Agent(IPAddress ipAddress)
            : this(ipAddress, 161, "public", "private")
        {

        }

        protected Agent(IPAddress ipAddress, int port)
            : this(ipAddress, port, "public", "private")
        {

        }

        protected Agent(IPAddress ipAddress, int port, string readCommunity, string writeCommunity)
        {
            IpAddress = ipAddress;
            Port = port;
            ReadCommunity = readCommunity;
            WriteCommunity = writeCommunity;
        }
        #endregion

        /// <summary>
        ///     刷新设备数据
        /// </summary>
        public void Refrash()
        {
            WorkerEngine.AddWork(this);
        }

        /// <summary>
        ///     该方法供 WorkProcesser 掉用
        /// </summary>
        public virtual void RefrashData()
        {
            var reasult = SnmpGet(SnmpVersion.Ver1, new string[] { });
            if (reasult == null)
            {
                if (!IsOnline)
                    return;
                IsOnline = false;
                OnOnlineChanged(new AgentOnlineChangedEventArgs(this));
            }
            else
            {
                if (!IsOnline)
                {
                    IsOnline = true;
                    OnOnlineChanged(new AgentOnlineChangedEventArgs(this));
                }
                foreach (KeyValuePair<Oid, AsnType> keyValuePair in reasult)
                {
                    Console.WriteLine(keyValuePair.Key.ToString() + "=>" + keyValuePair.Value);
                }
            }
        }

        public virtual Dictionary<Oid, AsnType> SnmpGet(SnmpVersion snmpVersion, string[] strings)
        {
            SimpleSnmp simpleSnmp = new SimpleSnmp(IpAddress.ToString(), Port, ReadCommunity);
            var reasult = simpleSnmp.Get(snmpVersion, strings);
            return reasult;
        }
    }
}
